package ocumed.persistenz.mapper;

import java.util.Calendar;
import java.util.List;

import ocumed.persistenz.dao.BehandlungDAO;
import ocumed.persistenz.dao.BenutzerDAO;
import ocumed.persistenz.dao.DAOFactory;
import ocumed.persistenz.dao.OrtDAO;
import ocumed.persistenz.dao.PatientDAO;
import ocumed.persistenz.dao.RezeptDAO;
import ocumed.persistenz.hibernate.OcBehandlung;
import ocumed.persistenz.hibernate.OcBenutzer;
import ocumed.persistenz.hibernate.OcOrt;
import ocumed.persistenz.hibernate.OcPatient;
import ocumed.persistenz.hibernate.OcRezept;
import ocumed.teams.IBehandlung;
import ocumed.teams.IMediver;
import ocumed.teams.IPatient;
import ocumed.teams.IPosition;
import ocumed.teams.IRezept;

public class PatientMapper extends PersistenceMapper {

	public void delete(Object instance) {
		PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
		pDAO.start();

		IPatient i = (IPatient) instance;
		OcPatient r = pDAO.findById(i.getPatientid());

		pDAO.delete(r);
		pDAO.commit();
	}

	/**
	 * override
	 *
	 * @see ocumed.persistenz.mapper.PersistenceMapper#get(int)
	 */
	@Override
	public Object get(int id) {
		PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
		pDAO.start();
		return pDAO.findById(id);
	}

	/**
	 * override
	 *
	 * @see ocumed.persistenz.mapper.PersistenceMapper#getAll()
	 */
	@Override
	public List getAll() {
		PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
		pDAO.start();
		return pDAO.findAll();
	}

	/**
	 * override
	 *
	 * @see ocumed.persistenz.mapper.PersistenceMapper#put(java.lang.Object)
	 */
	@Override
	public void put(Object instance) {
		PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
		pDAO.start();

		IPatient i = (IPatient) instance;
		OcPatient r = pDAO.findById(i.getPatientid());

		if (r != null) {
			updatePatient(r, i);
			pDAO.start();
			pDAO.attachDirty(r);
		} else {
			r = new OcPatient();
			updatePatient(r, i);
			Integer id = pDAO.persist(r);
			i.setPatientid(id);
		}

		pDAO.commit();
		updateReferences(i);
	}

	/**
	 * @param p
	 * @param i
	 */
	private void updatePatient(OcPatient ocPatient, IPatient i) {
    	PersistenceHandler ph = new PersistenceHandler();
		PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
		BenutzerDAO uDAO = DAOFactory.get(DAOFactory.RDBMS).getBenutzerDAO();
		OrtDAO oDAO = DAOFactory.get(DAOFactory.RDBMS).getOrtDAO();
		
		pDAO.start();
		uDAO.start();
		oDAO.start();

		OcBenutzer u = i.getiBenutzerByArztid() != null ? uDAO.findById(i
				.getiBenutzerByArztid().getBenutzerid()) : null;
		OcOrt o = null;
		
		if (i.getiOrt() != null) {
			o = oDAO.findById(i.getiOrt().getOrtid());
		}
		
		Calendar time = Calendar.getInstance();
		time.setTime(i.getPatientgebdatum());

		ocPatient.setPatientanrede(i.getPatientanrede());
		ocPatient.setPatientnachname(i.getPatientnachname());
		ocPatient.setPatientvorname(i.getPatientvorname());
		ocPatient.setGeandertam(Calendar.getInstance());
		ocPatient.setOcBenutzerByArztid(u);
		ocPatient.setOcBenutzerByGeandertvonid(u); // TODO ????
		ocPatient.setOcOrt(o);
		ocPatient.setPatientadresse(i.getPatientadresse());
		ocPatient.setPatientgeschlecht(i.getPatientgeschlecht());
		ocPatient.setPatientbesitztecard(i.getPatientbesitztecard());
		ocPatient.setPatientfax(i.getPatientfax());
		ocPatient.setPatientgebdatum(time);
		ocPatient.setPatientstatus(PatientDAO.typErfasst);
		ocPatient.setPatientsvn(i.getPatientsvn());
		ocPatient.setPatienttel(i.getPatienttel());
	}
	
	
    private void updateReferences(IPatient i) {
    	PersistenceHandler ph = new PersistenceHandler();
    	
		for (IBehandlung ib : i.getiBehandlungs()) {
			ph.put(ib);
		}

		for (IMediver im : i.getiMedivers()) {
			ph.put(im);
		}
    }

}
